<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;
use app\admin\model\Admin as AdminModel;


class Admin extends Common
{
    public function add(){
        if(request()->isPost()){
            $username=input('username');
            $password= md5(input("password"));
            $data['username']=$username;
            $data['password']=$password;
            $data['group_id']=input('group_id');
            $admin = new AdminModel();
            $res=$admin->addAdmin($data);
            if($res){
                return $this->success("添加成功","lst");
            } else {
                return $this->error("添加失败");    
            }
        }
        $auth=db('auth_group')->select();
        $this->assign("auth",$auth);
        return $this->fetch("add");
    }
    public function lst(){
        $admin=new AdminModel();
        $res=$admin->getAdmin(null);
        $auth=new Auth();
        foreach ($res as $k=>$v){
            $role=$auth->getGroups($v['id']);
            $group_title=$role[0]['title'];
            $v['group_title']=$group_title;
        }
        $this->assign("res",$res);
        return view();
    }
    public function del($id){
        $admin=new adminModel();
        $res=$admin->deleteAdmin($id);
        if($res){
            return $this->success("删除成功",'lst');
        }
        else{
            return $this->error("删除失败",'lst');
        }
    }
    
    public function edit($id){
        $admin=new AdminModel();
        $res=$admin->getAdmin($id);
        $this->assign("res",$res);
        if(request()->isPost()){

            $data=[];
            $data['id']=  (int)input('id');
            $data['username']= input("username");
            $data['password']= md5(input("password"));
            $data['group_id']=input('group_id');

            $res=$admin->saveAdmin($data);
            if($res){
                return $this->success("修改成功","lst");
            }else{
                return $this->error("修改失败","edit");
            }
        }

        $uid=db("auth_group_access")->where('uid',$id)->find();
        $this->assign('uid',$uid);
        $auth=db('auth_group')->select();
        $this->assign("auth",$auth);
        return view('edit');
    }
  
}